﻿#region namespaces

using System;
using System.Xml.Linq;
using ZOIL.DataModel;

#endregion

namespace ZOIL.DataBackend.BaseX
{
    internal static class SerializationHelper
    {
        public static Type FindTypeByNamespace(string ns, string className)
        {
            return Type.GetType(string.Format("{0}.{1}", ns, className));
        }

        public static object Deserialize(XElement serializedObjectWithPathAsAttribute, DatabaseConfiguration dbConfig)
        {
            var ns = dbConfig.ReadAttributeValueWithPathNamespace(serializedObjectWithPathAsAttribute, "Path");
//            var type = Type.GetType(ns);
            Type type;
            if (!TypeFinder.TryFindType(ns, out type))
            {
                throw new ArgumentException(string.Format("Passed serialized Object with Type.FullName {0} is not a existing type!", ns),
                                            "serializedObjectWithPathAsAttribute");
            }

            var deserialize = dbConfig.Serializer.Deserialize(serializedObjectWithPathAsAttribute, type);
            return deserialize;
        }
    }
}